﻿using System;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace WpfApp1;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private Color[] colors = { Colors.Red, Colors.Blue, Colors.Green, Colors.Black };
    private double[] brushSizes = { 2, 5, 10 };

    public MainWindow()
    {
        InitializeComponent();
        InitializeDrawingBoard();
    }

    private void InitializeDrawingBoard()
    {
        // 设置默认颜色和笔刷大小
        colorPicker.SelectedIndex = 0;
        sizePicker.SelectedIndex = 1;
        
        // 设置默认笔刷
        UpdateBrush();
        
        // 绑定事件
        colorPicker.SelectionChanged += (s, e) => UpdateBrush();
        sizePicker.SelectionChanged += (s, e) => UpdateBrush();
        saveButton.Click += SaveDrawing;
        clearButton.Click += ClearDrawing;
    }

    private void UpdateBrush()
    {
        var brush = new SolidColorBrush(colors[colorPicker.SelectedIndex]);
        brush.Freeze();
        drawingCanvas.DefaultDrawingAttributes.Color = brush.Color;
        drawingCanvas.DefaultDrawingAttributes.Width = brushSizes[sizePicker.SelectedIndex];
        drawingCanvas.DefaultDrawingAttributes.Height = brushSizes[sizePicker.SelectedIndex];
    }

    private void SaveDrawing(object sender, RoutedEventArgs e)
    {
        var renderTarget = new RenderTargetBitmap(
            (int)drawingCanvas.ActualWidth,
            (int)drawingCanvas.ActualHeight,
            96, 96, PixelFormats.Pbgra32);
        renderTarget.Render(drawingCanvas);

        var encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(renderTarget));

        // 添加水印
        var visual = new DrawingVisual();
        using (var dc = visual.RenderOpen())
        {
            dc.DrawImage(renderTarget, new Rect(0, 0, renderTarget.Width, renderTarget.Height));
            dc.DrawText(
                new FormattedText($"小画家作品 - {DateTime.Now:yyyy-MM-dd}",
                    CultureInfo.CurrentCulture,
                    FlowDirection.LeftToRight,
                    new Typeface("Arial"),
                    20,
                    Brushes.Red,
                    96),
                new Point(10, 10));
        }

        var finalBitmap = new RenderTargetBitmap(
            (int)drawingCanvas.ActualWidth,
            (int)drawingCanvas.ActualHeight,
            96, 96, PixelFormats.Pbgra32);
        finalBitmap.Render(visual);

        encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(finalBitmap));

        var saveDialog = new SaveFileDialog
        {
            Filter = "PNG 图片|*.png",
            DefaultExt = ".png",
            FileName = $"绘画作品_{DateTime.Now:yyyyMMddHHmmss}"
        };

        if (saveDialog.ShowDialog() == true)
        {
            using (var stream = saveDialog.OpenFile())
            {
                encoder.Save(stream);
            }
        }
    }

    private void ClearDrawing(object sender, RoutedEventArgs e)
    {
        drawingCanvas.Strokes.Clear();
    }
}